var but = document.getElementById("activdownloadbutton");

var box = document.createElement("div");
box.style.backgroundColor = "#000000";
box.style.color = "#FFFFFF";
box.style.width = "100%";
box.style.height = "100%";
box.style.position = "absolute";
box.style.top = "0";
box.style.left = "0";
box.style.textAlign = "center";
box.style.fontSize = "12px";
box.style.fontFamily = "'Lucida Grande', Arial, Helvetica, sans-serif";
box.style.zIndex = "10000";
document.body.appendChild(box);

var logo = document.createElement("div");
logo.style.paddingTop = "50px";
logo.align = 'center';
box.appendChild(logo);
logo.innerHTML = "<img src='http://www.cuevana.tv/img/logo.png' />";

var box2 = document.createElement("div");
box2.style.paddingTop = "50px";
box.appendChild(box2);
document.body.style.overflow = "hidden";

if (but) {
	box2.appendChild(but);
	
	but.innerHTML = "Click aquí para cargar el video";
	but.className = '';
	but.style.background = "#333";
	but.style.fontWeight = "bold";
	but.style.padding = "3px 7px";
	but.style.color = "#FFFFFF";
	but.style.textDecoration = "none";
	but.style.display = "inline";
	
	if (but.addEventListener) {
		but.addEventListener("click", clickBut, false);
	} else {
		but.attachEvent("onclick", clickBut);
	}
} else {
	var pag = document.body.innerHTML;
	if (pag.match(/Sorry, you cant download more then 1 files at time/i)) {
		box2.innerHTML = "Ya estás descargando un archivo desde Bitshare. <br />No puedes cargar más de 1 archivo en simultáneo sin una cuenta premium en Bitshare.com (<a href='http://bitshare.com/myupgrade.html' target='_blank'>comprar una cuenta premium en Bitshare</a>).";
	} else if (pag.match(/File not available/i)) {
		box2.innerHTML = "El archivo ha sido eliminado de Bitshare.";
	} else if (document.getElementById("dlbutton")) {
		var b = document.getElementById("dlbutton").getElementsByTagName("a")[0];
		b.innerHTML = "Click aquí para cargar el video";
		b.className = '';
		b.style.background = "#333";
		b.style.fontWeight = "bold";
		b.style.padding = "3px 7px";
		b.style.color = "#FFFFFF";
		b.style.textDecoration = "none";
		b.style.display = "inline";
		box2.appendChild(b);
		getFinalLink();
	}
}

// Funciones
function clickBut(e) {
	if (but.removeEventListener) {
		but.removeEventListener("click", clickBut,false);
	} else {
		but.detachEvent("onclick", clickBut);
	}
	box2.innerHTML = "Cargando...";
	setTimeout("loadCaptcha()",200);
}
function loadCaptcha() {
	if (document.getElementById("counter")) {
		var m = parseInt(document.getElementById("counter").innerHTML);
		if (m>0) {
			box2.innerHTML = "Por favor, espera "+m+" segundos";
		} else {
			box2.innerHTML = "Cargando...";
			setTimeout("getLink()",500);
			return;
		}
	}
	setTimeout("loadCaptcha()",500);
}
function getCaptcha() {
	var form = document.getElementById("captcha");
	if (form) {
		box2.innerHTML = "";
		form.style.position = "fixed";
		form.style.top = "180px";
		form.style.zIndex = "10001";
		form.style.width = "420px";
		form.style.margin = "0 auto";
		var n = form.childNodes.length;
		var d = form.childNodes[n-4];
		d.style.width = "auto";
		d.style.height = "auto";
		var a = d.childNodes[1];
		a.innerHTML = "Enviar captcha";
		a.style.background = "#333";
		a.style.fontWeight = "bold";
		a.style.padding = "3px 7px";
		a.style.color = "#FFFFFF";
		a.style.textDecoration = "none";
		a.style.display = "inline";
		a.style.fontSize = "12px";
		var p = d.parentNode;
		p.replaceChild(a,d);
		p.removeChild(p.getElementsByTagName("b")[0]);
		var br = p.getElementsByTagName("br");
		p.removeChild(br[0]);
		p.removeChild(br[0]);
		setTimeout("getFinalLink()",500);
	}
	setTimeout("checkBanner()",500);
}
function getLink() {
	if (document.getElementById("captcha").style.display != 'none') {
		getCaptcha();
		return;
	} else {
		var a = document.getElementById("downloadbutton_link");
		if (a.href.match(/download.php/i)) {
			getFinalLink();
			return;
		}
	}
	setTimeout("getLink()",500);
}
function checkBanner() {
	if (document.getElementById("captcha_advertising")) {
		document.getElementById("captcha_advertising").innerHTML = '';
		return;
	}
	setTimeout("checkBanner()",500);
}
function getFinalLink() {
	var a = document.getElementById("downloadbutton_link");
	if (a.href.match(/download.php/i)) {
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).replace(/amp;/gi,'');
		var h = hashes.split('&'), hash, vars = [];
		for(var i=0;i<h.length;i++){
			hash = h[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		var id = vars['id'], tipo = (vars['tipo']=="s") ? 'series' : 'peliculas', epi = (tipo=='series') ? '/episodio' : '';
		window.location = 'http://www.cuevana.tv/'+tipo+'/'+id+epi+'/play/?megaurl='+a.href+'&count=20&'+hashes;
		return;
	}
	setTimeout("getFinalLink()",100);
}